package com.dragon8869.myspringboot.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OfficeVendorType;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.poserver.AdminSeal;
import com.zhuozhengsoft.pageoffice.poserver.Server;
import org.apache.commons.io.filefilter.SuffixFileFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("pageoffice")
public class PageOfficeController {

    @Value("${posyspath}")
    private String poSysPath;

    @Value("${popassword}")
    private String poPassWord;

    @RequestMapping("/hello")
    public String test() {
        System.out.println("hello run");
        return "Hello";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView showIndex() throws IOException {
        ClassPathResource resource = new ClassPathResource("file");
        File file = resource.getFile();
        FileFilter fileFilter = new SuffixFileFilter("doc", "docx", "ppt", "pptx", "xls", "xlsx");
        File[] files = file.listFiles(fileFilter);
        Map<String, File[]> map = new HashMap<>();
        map.put("files", files);
        return new ModelAndView("pageoffice/Index", map);
    }

    @RequestMapping(value = "/word", method = RequestMethod.GET)
    public ModelAndView showWord(HttpServletRequest request, Map<String, Object> map, @RequestParam(name = "fileName") String fileName) throws IOException {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setServerPage("/pageoffice/poserver.zz");//设置服务页面
        poCtrl.addCustomToolButton("保存", "Save", 1);//添加自定义保存按钮
        poCtrl.addCustomToolButton("盖章", "AddSeal", 2);//添加自定义盖章按钮
        poCtrl.setSaveFilePage("/pageoffice/save");//设置处理文件保存的请求方法
        poCtrl.setOfficeVendor(OfficeVendorType.AutoSelect);
        ClassPathResource resource = new ClassPathResource("file/" + fileName);
        String path = resource.getFile().getPath();
        OpenModeType openModeType = null;
        if (path.endsWith("doc") || path.endsWith("docx")) {
            openModeType = OpenModeType.docNormalEdit;
        } else if (path.endsWith("xls") || path.endsWith("xlsx")) {
            openModeType = OpenModeType.xlsNormalEdit;
        } else if (path.endsWith("ppt") || path.endsWith("pptx")) {
            openModeType = OpenModeType.pptNormalEdit;
        }
        if (openModeType == null) {
            throw new IllegalArgumentException("非office文档！");
        }
        poCtrl.webOpen(path, openModeType, "张三");
        map.put("pageoffice", poCtrl.getHtmlCode("PageOfficeCtrl1"));
        return new ModelAndView("pageoffice/Word", map);
    }

    @RequestMapping("/save")
    public void saveFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
        FileSaver fs = new FileSaver(request, response);
        ClassPathResource resource = new ClassPathResource("file");
        File file = new File(resource.getFile(), fs.getFileName());
        fs.saveToFile(file.getPath());
        fs.close();
    }


    /**
     * 添加PageOffice的服务器端授权程序Servlet（必须）
     *
     *
     */
    @Bean
    public ServletRegistrationBean<Server> servletRegistrationBean() {
        com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();
        poserver.setSysPath(poSysPath);//设置PageOffice注册成功后,license.lic文件存放的目录
        ServletRegistrationBean<Server> srb = new ServletRegistrationBean<>(poserver);
        srb.addUrlMappings("/pageoffice/poserver.zz");
        srb.addUrlMappings("/pageoffice/posetup.exe");
        srb.addUrlMappings("/pageoffice/pageoffice.js");
        srb.addUrlMappings("/pageoffice/jquery.min.js");
        srb.addUrlMappings("/pageoffice/pobstyle.css");
        srb.addUrlMappings("/pageoffice/sealsetup.exe");
        return srb;// 
    }

    /**
     * 添加印章管理程序Servlet（可选）
     *
     *
     */
    @Bean
    public ServletRegistrationBean<AdminSeal> servletRegistrationBean2() {
        com.zhuozhengsoft.pageoffice.poserver.AdminSeal adminSeal = new com.zhuozhengsoft.pageoffice.poserver.AdminSeal();
        adminSeal.setAdminPassword(poPassWord);//设置印章管理员admin的登录密码
        adminSeal.setSysPath(poSysPath);//设置印章数据库文件poseal.db存放的目录
        ServletRegistrationBean<AdminSeal> srb = new ServletRegistrationBean<>(adminSeal);
        srb.addUrlMappings("/pageoffice/adminseal.zz");//印章管理
        srb.addUrlMappings("/pageoffice/sealimage.zz");//新增印章上传图片
        srb.addUrlMappings("/pageoffice/loginseal.zz");//印章管理登陆
        //http://localhost:8080/pageoffice/adminseal.zz 访问印章管理
        //签章时需要输入用户名（签章人姓名）和密码（新添加的电子印章的默认密码为“111111”）
        return srb;// 
    }
}
